﻿Imports Cls_Common
Imports VSModel
Imports System.Data

Partial Class FrontEnd_Control_RaoVat_TinBanXeSEO
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Page.Title = "ban xe oto cu tai tphcm | oto sai thanh"
            Page.MetaDescription = "cần bán xe oto cu tphcm, cần bán gấp xe oto cũ giá rẻ tphcm"
            Page.MetaKeywords = "cần bán xe ô tô cũ giá rẻ chính chủ tại tphcm, can ban gap xe oto cu gia re tai tphcm"
            BindToGrid(1, 13)
        End If
    End Sub
    Private Sub BindToGrid(ByVal ipage As Integer, ByVal _TinhThanhId As Integer)
        Using Data As New VSEntities
            Dim p = (From q In Data.RaoVats Where q.TinhTP = _TinhThanhId And q.StatusId = 3 _
                                                And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
                                                Or q.eEffectiveDate Is Nothing) _
                                            Order By q.PublishedDate Descending, q.Created Descending Select q).Take(50).ToList

            With rptProduct
                .DataSource = p
                .DataBind()
            End With

        End Using
    End Sub

    Protected Sub rtpProduct_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptProduct.ItemDataBound
        Dim row As VSModel.RaoVat = CType(e.Item.DataItem, VSModel.RaoVat)

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        Dim ltrTinhTrangXe As Literal = CType(e.Item.FindControl("ltrTinhTrangXe"), Literal)


        ltrTinhTrangXe.Text = getTinhTrangXe(row.RaovatId)

        hplTitle.Text = row.Title.Split("(")(0)
        hplTitle.NavigateUrl = MainDomain & row.QueryString & "-" & row.RaovatId.ToString & ".html"
        Dim ltrDescription As Literal = CType(e.Item.FindControl("ltrDescription"), Literal)
        ltrDescription.Text = CropText(row.Description, 100, "...")


        Dim imgProduct As Image = CType(e.Item.FindControl("imgProduct"), Image)

        imgProduct.ImageUrl = CookedImageURL("84-84", row.ImagePreview.ToString)
        imgProduct.AlternateText = row.Title


        Dim ltrPrice As Literal = CType(e.Item.FindControl("ltrPrice"), Literal)
        ltrPrice.Text = "Giá bán: " & IIf(row.OriginalPrice = 0, "Call", FormatCurrencyOto(row.OriginalPrice.ToString))

        Dim ltrLienHe As Literal = CType(e.Item.FindControl("ltrLienHe"), Literal)

        Dim ltrTelephone As Literal = CType(e.Item.FindControl("ltrTelephone"), Literal)
        ltrLienHe.Text = row.HoTen & Environment.NewLine & row.DiaChi
        ltrTelephone.Text = row.Phone

        If Not row.PhoneOther Is Nothing AndAlso row.PhoneOther <> "" Then
            ltrTelephone.Text = ltrTelephone.Text & " - " & row.PhoneOther
        End If


    End Sub
    Private Function getTinhTrangXe(ByVal _RaoVatId As Integer) As String
        Using _data As New VSEntities
            Dim p = (From q In _data.ThongSoCoBans Where q.RaoVatId = _RaoVatId Select q.TinhTrang, q.NamSanXuat).FirstOrDefault

            If Not p Is Nothing AndAlso p.TinhTrang = 1 Then
                Return " Xe mới " & p.NamSanXuat.ToString
            ElseIf Not p Is Nothing Then
                Return " Xe cũ " & p.NamSanXuat.ToString
            End If
        End Using
        Return ""
    End Function

    


End Class
